package com.shopping.mall.core.module.enterprise.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shopping.mall.core.module.enterprise.entity.Enterprise;
import com.shopping.mall.core.module.enterprise.mapper.EnterpriseMapper;
import com.shopping.mall.core.module.enterprise.service.EnterpriseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shopping.mall.base.http.ResponseState;
import com.shopping.mall.base.http.Result;
import com.shopping.mall.base.util.WrapperUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;
/**
 * <p>
 *  Enterprise服务实现类
 * </p>
 *
 * @author zzy
 * @since 2021-07-31
 */
@Validated
@Service
public class EnterpriseServiceImpl extends ServiceImpl<EnterpriseMapper, Enterprise> implements EnterpriseService {

    @Autowired
    private EnterpriseMapper enterpriseMapper;

    /**
     * 分页查询
     * @param current：页码
     * @param size：每页条数
     * @param enterprise：筛选条件
     * @return IPage<Enterprise>
     */
    @Override
    public IPage<Enterprise> getEnterprisePage(Integer current,Integer size,Enterprise enterprise){
        QueryWrapper<Enterprise> queryWrapper = WrapperUtils.queryWrapper(enterprise);
        Page<Enterprise> enterprisePage = new Page<>(current,size);
        IPage<Enterprise> enterpriseIPage = enterpriseMapper.selectPage(enterprisePage, queryWrapper);
        return enterpriseIPage;
    }

    /**
     * 查询所有
     * @param enterprise：筛选条件
     * @return <List<Enterprise>
     */
    @Override
    public List<Enterprise> getAll(Enterprise enterprise){
        QueryWrapper<Enterprise> queryWrapper = WrapperUtils.queryWrapper(enterprise);
        return enterpriseMapper.selectList(queryWrapper);
    }

    /**
     * 通过id删除
     * @param id：enterpriseId
     * @return Result<Object>
     */
    @Override
    public Result<Object> deleteById(String id){
        Enterprise enterprise=enterpriseMapper.selectById(id);
        if(enterprise==null){
            return Result.error(ResponseState.TARGET_NOT_EXIST.getValue(), ResponseState.TARGET_NOT_EXIST.getMessage());
        }
        enterpriseMapper.deleteById(id);
        return Result.OK();
    }

    /**
    * 批量删除
    * @param ids：id列表
    * @return Result<Object>
    */
    @Override
    public Result<Object> batchDelete(List<String> ids){
        UpdateWrapper<Enterprise> updateWrapper=new UpdateWrapper<>();
        updateWrapper.in("id",ids);
        enterpriseMapper.delete(updateWrapper);
        return Result.OK();
    }
}